package com.kkb.shopcenter.controller;

import com.kkb.shopcenter.api.MenuInfoService;
import com.kkb.shopcenter.api.RoleInfoService;
import com.kkb.shopcenter.api.UserInfoService;
import com.kkb.shopcenter.common.common.HttpResult;
import com.kkb.shopcenter.common.pojo.MenuInfo;
import com.kkb.shopcenter.common.pojo.MenuVo;
import com.kkb.shopcenter.common.pojo.UserInfo;
import com.kkb.shopcenter.common.utils.SecurityUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/menus")
public class MenuController  {
    @Reference
    private UserInfoService userInfoService;

    @Reference
    private MenuInfoService menuInfoService;

    @Reference
    private RoleInfoService roleInfoService;

    @GetMapping(value = "/build")
    @ResponseBody
    public HttpResult buildMenus(){

        UserInfo user = userInfoService.findByUsername(SecurityUtils.getUserName());

        List<MenuInfo> menuDtoList = menuInfoService.findByRoles(roleInfoService.selectRolesByUserId(user.getId()));
        List<MenuInfo> menuDtos = (List<MenuInfo>) menuInfoService.buildTree(menuDtoList).get("content");
        List<MenuVo> menuVos = menuInfoService.buildMenus(menuDtos);

        return HttpResult.success(menuVos);
    }

    @GetMapping(value = "/tree")
    @ResponseBody
    public HttpResult getMenuTree(){
        List<Map<String, Object>> list = menuInfoService.getMenuTree(menuInfoService.findByPid(0L));
        return HttpResult.success(list);
    }
 }
